#include "graph.h"
#include <stdio.h>
#include <stdlib.h>

// 基本功能测试程序
int main() {
    printf("=== C语言核心算法基本测试 ===\n\n");
    
    // 1. 测试CSV加载
    printf("1. 测试CSV数据加载...\n");
    Graph* g = load_graph_from_csv("data/CSV-version/Conv_Case0_Nodes.csv", "data/CSV-version/Conv_Case0_Edges.csv");
    if (!g) {
        printf("❌ CSV加载失败\n");
        return 1;
    }
    printf("✅ CSV加载成功\n");
    
    // 2. 测试基本图统计
    printf("\n2. 图统计信息:\n");
    print_graph_stats(g);
    
    // 3. 测试传递闭包
    printf("\n3. 测试传递闭包计算...\n");
    verify_transitive_closure(g);
    
    // 4. 测试successors/predecessors函数
    printf("\n4. 测试图操作函数...\n");
    int succ_count, pred_count;
    int* successors = get_successors(g, 0, &succ_count);
    int* predecessors = get_predecessors(g, 10, &pred_count);
    
    printf("节点0的后继数量: %d\n", succ_count);
    printf("节点10的前驱数量: %d\n", pred_count);
    
    if (successors) free_node_list(successors);
    if (predecessors) free_node_list(predecessors);
    
    // 5. 测试初始调度生成
    printf("\n5. 测试初始调度生成...\n");
    Schedule* initial = generate_initial_schedule(g);
    if (!initial) {
        printf("❌ 初始调度生成失败\n");
        free_graph(g);
        return 1;
    }
    printf("✅ 初始调度生成成功\n");
    print_schedule_stats(initial);
    
    // 6. 测试L0约束验证
    printf("\n6. 测试L0约束验证...\n");
    bool is_valid = is_l0_schedule_valid(initial->schedule, initial->length, g);
    printf("初始调度L0约束验证: %s\n", is_valid ? "✅ 通过" : "❌ 失败");
    
    // 7. 测试V_stay计算
    printf("\n7. 测试V_stay计算...\n");
    int* test_history = malloc(initial->length * sizeof(int));
    int test_vstay = calculate_max_vstay(initial->schedule, initial->length, g, test_history);
    printf("V_stay峰值: %d\n", test_vstay);
    printf("与初始调度一致性: %s\n", 
           (test_vstay == initial->max_vstay) ? "✅ 一致" : "❌ 不一致");
    
    // 8. 输出测试结果到文件
    printf("\n8. 输出测试结果...\n");
    print_transitive_closure(g, "test_transitive_closure.txt");
    
    FILE* test_fp = fopen("test_results.txt", "w");
    if (test_fp) {
        fprintf(test_fp, "=== C语言核心算法测试结果 ===\n");
        fprintf(test_fp, "节点数: %d\n", g->num_nodes);
        fprintf(test_fp, "边数: %d\n", g->num_edges);
        fprintf(test_fp, "初始调度长度: %d\n", initial->length);
        fprintf(test_fp, "初始V_stay峰值: %d\n", initial->max_vstay);
        fprintf(test_fp, "L0约束有效性: %s\n", is_valid ? "有效" : "无效");
        
        fprintf(test_fp, "\n前10个节点信息:\n");
        for (int i = 0; i < 10 && i < g->num_nodes; i++) {
            GraphNode* node = &g->nodes[i];
            fprintf(test_fp, "节点%d: Op=%d, Type=%d, Size=%d, Bufs_count=%d\n", 
                   node->id, node->op_code, node->type_code, node->size, node->bufs_count);
        }
        
        fprintf(test_fp, "\n前20步调度序列:\n");
        for (int i = 0; i < 20 && i < initial->length; i++) {
            fprintf(test_fp, "步骤%d: 节点%d, V_stay=%d\n", 
                   i + 1, initial->schedule[i], test_history[i]);
        }
        
        fclose(test_fp);
        printf("✅ 测试结果已保存到 test_results.txt\n");
    }
    
    // 清理内存
    free(test_history);
    free_schedule(initial);
    free_graph(g);
    
    printf("\n=== 基本测试完成 ===\n");
    printf("如果所有测试都显示✅，则核心功能实现正确！\n");
    
    return 0;
}
